package com.zhangxun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangxun.annotation.ShopLog;
import com.zhangxun.domain.SysRole;
import com.zhangxun.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("list")
    @ApiOperation("查询所有的权限")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public ResponseEntity<List> roleList(){

        return ResponseEntity.ok(sysRoleService.list());
    }

    @ShopLog(operation = "查询所有的权限")
    @GetMapping("page")
    @ApiOperation("查询所有的权限")
    @PreAuthorize("hasAuthority('sys:role:page')")
    public ResponseEntity<Page> rolePage(Page<SysRole> page,String roleName){


        return ResponseEntity.ok(sysRoleService.page(page,new LambdaQueryWrapper<SysRole>()
                .eq(StringUtils.hasText(roleName), SysRole::getRoleName, roleName)));
    }
}
